package com.tanhua.app.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.api.FriendApi;
import com.tanhua.api.UserApi;
import com.tanhua.api.UserInfoApi;
import com.tanhua.app.exception.BusinessException;
import com.tanhua.app.exception.ErrorResult;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.commons.utils.Constants;
import com.tanhua.config.template.HuanXinTemplate;
import com.tanhua.model.pojo.User;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessagesService {
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private UserApi userApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private FriendApi friendApi;
    //根据环信id查询用户详情
    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        //1.根据环信id查询用户user数据
        User user=userApi.findByHuanxinId(huanxinId);

        //2.根据用户的id查询用户详情 userinfo
        UserInfo userInfo = userInfoApi.findById(user.getId());

        //3.封装vo
        /*UserInfoVo vo= new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo);
        if (userInfo.getAge()!=null){
            vo.setAge(userInfo.getAge().toString());
        }*/
        UserInfoVo vo = UserInfoVo.init(userInfo);
        //返回结果

        return vo;
    }


    //添加好友
    public void contacts(Long friendId) {
        //1.在环信中注册好友关系
        Boolean aBoolean = huanXinTemplate.addContact(
                Constants.HX_USER_PREFIX+UserHolder.getUserId(),
                Constants.HX_USER_PREFIX+ friendId);
        if (!aBoolean){
            throw new BusinessException(ErrorResult.contactsError());
        }
        //2.调用api保存双向的好友关系
        friendApi.save(UserHolder.getUserId(),friendId);
    }

    //删除好友
    public void deleteContacts(Long friendId) {
        //1.在环信中删除好友关系
        Boolean aBoolean = huanXinTemplate.addContact(
                Constants.HX_USER_PREFIX+UserHolder.getUserId(),
                Constants.HX_USER_PREFIX+ friendId);
        if (!aBoolean){
            throw new BusinessException(ErrorResult.contactsError());
        }
        //2.调用api保存双向的好友关系
        friendApi.delete(UserHolder.getUserId(),friendId);
    }


    //根据昵称模糊查询,分页查询联系人
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        //1.查询用户联系人(好友)
       List<Long> ids = friendApi.findFriends(UserHolder.getUserId());
        //2.判断查询结果是否为空,为空直接返回
        //if (ids ==null || ids.isEmpty()){
        if (CollUtil.isEmpty(ids)){
            return new PageResult(page,pagesize,0,null);
        }
        //3.根据好友的ids 昵称的关键词 分页查询好友的用户详情
        List<UserInfo> userInfoList =userInfoApi.findByPage(ids,keyword,page,pagesize);
        //4.封装vos
        List<ContactVo> vos = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            ContactVo vo = ContactVo.init(userInfo);
            vos.add(vo);
        }
        //5.封装分页对象,返回数据
        return new PageResult(page,pagesize,0,vos);
    }
}
